﻿namespace KeRui.Upload
{
    using System;
    using System.IO;
    using System.Text;
    using System.Web;

    public class UploadedFile
    {
        private string mContentType;
        private Stream mDataStream;
        private Stream mInputStream;
        private KeRui.Upload.MacBinary mMacBinary;
        private byte[] mRawFileName;
        private Stream mRawStream;
        private Stream mRezStream;
        private KeRui.Upload.TempFile mTempFile;
        private KeRui.Upload.Upload mUpload;

        internal UploadedFile(KeRui.Upload.Upload upload, Multipart.Field file)
        {
            Registration.CheckValid();
            this.mInputStream = file.TempFile.FileStream;
            this.mContentType = file.MIMEType;
            this.mRawFileName = file.FileName;
            this.mTempFile = file.TempFile;
            this.mUpload = upload;
            this.UpdateStreams();
        }

        internal UploadedFile(KeRui.Upload.Upload upload, HttpPostedFile file)
        {
            Registration.CheckValid();
            this.mInputStream = file.InputStream;
            this.mContentType = file.ContentType;
            this.mRawFileName = Encoding.GetEncoding(upload.CodePage).GetBytes(file.FileName);
            this.mTempFile = null;
            this.mUpload = upload;
            this.UpdateStreams();
        }

        public string Detach()
        {
            string str;
            try
            {
                string str2 = "";
                if (this.mTempFile != null)
                {
                    str2 = this.mTempFile.Detach();
                    this.mTempFile = null;
                }
                str = str2;
            }
            catch (Exception exception)
            {
                if (Settings.Current.Advanced.LogExceptions)
                {
                    Log.ExceptionDetails(exception);
                }
                throw exception;
            }
            return str;
        }

        public void SaveAs(string path)
        {
            try
            {
                path = this.mUpload.ResolvePath(path);
                if (this.mMacBinary != null)
                {
                    MacFile file = new MacFile(this.mMacBinary);
                    file.SetStreams(false);
                    file.SetOverwrite(this.mUpload.Overwrite);
                    file.Save(path);
                }
                else
                {
                    FileUtils.WriteDataToFile(path, this.mUpload.Overwrite, this.mDataStream);
                }
            }
            catch (Exception exception)
            {
                if (Settings.Current.Advanced.LogExceptions)
                {
                    Log.ExceptionDetails(exception);
                }
                throw exception;
            }
        }

        public void SaveSFM(string path)
        {
            try
            {
                path = this.mUpload.ResolvePath(path);
                if (this.mMacBinary != null)
                {
                    MacFile file = new MacFile(this.mMacBinary);
                    file.SetStreams(true);
                    file.SetOverwrite(this.mUpload.Overwrite);
                    file.Save(path);
                }
                else
                {
                    FileUtils.WriteDataToFile(path, this.mUpload.Overwrite, this.mDataStream);
                }
            }
            catch (Exception exception)
            {
                if (Settings.Current.Advanced.LogExceptions)
                {
                    Log.ExceptionDetails(exception);
                }
                throw exception;
            }
        }

        internal void UpdateStreams()
        {
            this.mRawStream = new SuperStream(this.mInputStream);
            this.mMacBinary = new KeRui.Upload.MacBinary();
            this.mMacBinary.SetData(new SuperStream(this.mRawStream), this.mContentType);
            if (this.mMacBinary.IsOK())
            {
                this.mDataStream = this.mMacBinary.DataStream;
                this.mRezStream = this.mMacBinary.RezStream;
            }
            else
            {
                this.mMacBinary = null;
                this.mDataStream = new SuperStream(this.mRawStream);
                this.mRezStream = new MemoryStream(0);
            }
            this.mRawStream.Position = this.mDataStream.Position = this.mRezStream.Position = 0L;
        }

        public long ContentLength
        {
            get
            {
                long length;
                try
                {
                    length = this.mInputStream.Length;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return length;
            }
        }

        public string ContentType
        {
            get
            {
                string mContentType;
                try
                {
                    mContentType = this.mContentType;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return mContentType;
            }
        }

        public Stream DataStream
        {
            get
            {
                Stream mDataStream;
                try
                {
                    mDataStream = this.mDataStream;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return mDataStream;
            }
        }

        public bool Exists
        {
            get
            {
                bool flag;
                try
                {
                    flag = this.mRawFileName.Length > 0;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return flag;
            }
        }

        public string Extension
        {
            get
            {
                string str;
                try
                {
                    string fileXt = "";
                    if (this.mMacBinary != null)
                    {
                        fileXt = this.mMacBinary.GetFileXt();
                    }
                    else
                    {
                        fileXt = Utilities.FindFileExtension(this.FileName);
                    }
                    if (fileXt != "")
                    {
                        fileXt = "." + fileXt;
                    }
                    str = fileXt;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return str;
            }
        }

        public string FileName
        {
            get
            {
                string rawPath;
                try
                {
                    rawPath = HTTPUtils.GetRawPath(this.mRawFileName, this.mUpload.CodePage);
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return rawPath;
            }
        }

        public Stream InputStream
        {
            get
            {
                Stream mInputStream;
                try
                {
                    mInputStream = this.mInputStream;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return mInputStream;
            }
        }

        public bool MacBinary
        {
            get
            {
                bool flag;
                try
                {
                    flag = this.mMacBinary != null;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return flag;
            }
        }

        public byte[] RawFileName
        {
            get
            {
                byte[] mRawFileName;
                try
                {
                    mRawFileName = this.mRawFileName;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return mRawFileName;
            }
        }

        public Stream RawStream
        {
            get
            {
                Stream mRawStream;
                try
                {
                    mRawStream = this.mRawStream;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return mRawStream;
            }
        }

        public Stream RezStream
        {
            get
            {
                Stream mRezStream;
                try
                {
                    mRezStream = this.mRezStream;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return mRezStream;
            }
        }

        public string TempFile
        {
            get
            {
                string path;
                try
                {
                    if ((this.mTempFile == null) || (this.mTempFile.Path == null))
                    {
                        return "";
                    }
                    path = this.mTempFile.Path;
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return path;
            }
        }

        public string Text
        {
            get
            {
                string str;
                try
                {
                    str = Utilities.StreamToString(this.DataStream, this.mUpload.CodePage);
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return str;
            }
        }

        public string URLSafeFileName
        {
            get
            {
                string uRLSafeFileName;
                try
                {
                    uRLSafeFileName = HTTPUtils.GetURLSafeFileName(this.mRawFileName, this.mUpload.CodePage);
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return uRLSafeFileName;
            }
        }

        public string WinSafeFileName
        {
            get
            {
                string winSafeFileName;
                try
                {
                    winSafeFileName = HTTPUtils.GetWinSafeFileName(this.mRawFileName, this.mUpload.CodePage);
                }
                catch (Exception exception)
                {
                    if (Settings.Current.Advanced.LogExceptions)
                    {
                        Log.ExceptionDetails(exception);
                    }
                    throw exception;
                }
                return winSafeFileName;
            }
        }
    }
}

